﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Core.Events;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TGPDAT : ViewBase
    {

        public VGPDAT TMV
        {
            get
            {
                return (VGPDAT)MV;
            }
        }
        #region ==QPTYP===========
        public static readonly DependencyProperty QPTYPProperty = DependencyProperty.Register("QPTYP",
            typeof(string), typeof(TGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPTYPChanged));

        public string QPTYP
        {
            get
            {
                return (string)GetValue(QPTYPProperty);
            }
            set
            {
                SetValue(QPTYPProperty, value);
                TMV.QPTYP = value;
            }
        }

        private static void OnQPTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.QPTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                TMV.QPLAN = value;
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKEY===========
        public static readonly DependencyProperty QPKEYProperty = DependencyProperty.Register("QPKEY",
            typeof(string), typeof(TGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKEYChanged));
        public string QPKEY
        {
            get
            {
                return (string)GetValue(QPKEYProperty);
            }
            set
            {
                SetValue(QPKEYProperty, value);

            }
        }
        private static void OnQPKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.QPKEY = (string)e.NewValue;
            }
        }
        #endregion


        #region ==QPKRS===========
        public static readonly DependencyProperty QPKRSProperty = DependencyProperty.Register("QPKRS",
            typeof(string), typeof(TGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKRSChanged));
        public string QPKRS
        {
            get
            {
                return (string)GetValue(QPKRSProperty);
            }
            set
            {
                SetValue(QPKRSProperty, value);
                TMV.QPKRS = value;
            }
        }
        private static void OnQPKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.QPKRS = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwGP===========
        public static readonly DependencyProperty dwGPProperty = DependencyProperty.Register("dwGP",
            typeof(DataRow), typeof(TGPDAT),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGPChanged));
        public DataRow dwGP
        {
            get
            {
                return (DataRow)GetValue(dwGPProperty);
            }
            set
            {
                SetValue(dwGPProperty, value);
                if (MV == null) return;
                TMV.dwGP = value;
            }
        }
        private static void OdwGPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGP = row;
            }
        }
        #endregion

        #region ==FTAUTOQC===========
        public static readonly DependencyProperty FTAUTOQCProperty = DependencyProperty.Register("FTAUTOQC",
            typeof(bool), typeof(TGPDAT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFTAUTOQCChanged));
        public bool FTAUTOQC
        {
            get
            {
                return (bool)GetValue(FTAUTOQCProperty);
            }
            set
            {
                SetValue(FTAUTOQCProperty, value);
                TMV.FTAUTOQC = value;
            }
        }
        private static void OnFTAUTOQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.FTAUTOQC = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==QCORDER===========
        public static readonly DependencyProperty QCORDERProperty = DependencyProperty.Register("QCORDER",
            typeof(string), typeof(TGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQCORDERChanged));
        public string QCORDER
        {
            get
            {
                return (string)GetValue(QCORDERProperty);
            }
            set
            {
                SetValue(QCORDERProperty, value);
                TMV.QCORDER = value;
            }
        }
        private static void OnQCORDERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGPDAT;
            if (src != null)
            {
                src.QCORDER = (string)e.NewValue;
            }
        }
        #endregion

        public TGPDAT()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGPDAT();
            MESSAGES += "QCRELOAD;REFRESHBYGB33;LOADGP;";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }
        /// <summary> 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGP = dwMaster;
            if (dwGP == null)
            {
                QCORDER = "000000000";
                QPKEY = "";
            }
            else
            {
                QCORDER = dwGP["QOKORDER"].ToString();
                QPKEY = cQP02.QPKEY;
            }
        }
        MessageInfo LastMsg = null;
        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "QCRELOAD":
                    TBMain.Clear();
                    TMV.QCRELOAD(msg);
                    vwMaster.View.MoveCurrentToFirst();
                    break;
                case "LOADGP":
                    TBMain.Clear();
                    LOADGP(msg);
                    LastMsg = msg;
                    break;
                case "REFRESHBYGB33":
                    TMV.REFRESHBYGB33(msg);
                    break;
            }
        }

        public void LOADGP(MessageInfo msg)
        {
            TBMain.Clear();
            if (msg.PARTIE.Trim().Length == 0)
            {
                return;
            }
            TMV.QCRELOAD(msg);
            vwMaster.View.MoveCurrentToFirst();

        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (LastMsg == null)
                return;
            QCNEW(LastMsg);
            LastMsg = null;
            base.NewCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (LastMsg == null)
                return;
            DataRow dwPartie = VGPDAT.Instance.TBMain.NewRow();
            dwPartie["GPMANDANT"] = app.Mandant;
            dwPartie["GPPARTIE"] = LastMsg.PARTIE;
            dwPartie["GPGEPRUEFT"] = "J";
            VGPDAT.Instance.UpdateGPDAT(dwPartie, 2);
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwGP == null)
                return;
            VQOSTEP.Instance.UPDTEBYGB33(RUNID, dwGP);
            base.SaveCommand(sender, e);
        }

        public void QCNEW(MessageInfo _msg)
        {

            MessageInfo msg = new MessageInfo(_msg);
            msg.Set = _msg.Set;
            msg.MSGTYPE = "QCNEW";
            msg.Sender = this.RUNID;
            msg.ACTION = "QCNEW";
            msg.AUFTYP = "GB";
            if (QPLAN.Trim() == "")
                QPLAN = cQP02.KEY;
            if (QPLAN.Trim() == "")
                return;  
            PM["RUNID"] = RUNID;
            PM["ORDER"] = msg.ORDER;
            PM["P5"] = msg.P5;
            PM["AUFTYP"] = msg.AUFTYP;
            PM["KRS"] = msg.KRS;
            PM["LFD"] = msg.LFD;
            PM["POS"] = msg.POS;
            PM["LIEFSCH"] = msg.LIEFSCH;
            PM["ART"] = msg.ART;
            PM["VAR"] = msg.VAR;
            PM["FARBE"] = msg.FARBE;
            PM["PARTIE"] = msg.PARTIE;
            PM["DESS"] = msg.DESS;
            PM["COLORIT"] = msg.COLORIT;
            PM["BESCHICH"] = msg.BESCHICH;
            PM["GROE"] = msg.GROE;
            PM["QPLAN"] = QPLAN;
            PM["YCOUNT"] = msg.P1;
            PM["ROWQP"] = msg.Set.Tables[0].Rows[0];
            VQORDER.Instance.QCNEW(PM);

        }
    }
}
